package zz.demo.springCloud.wechat.service.impl;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zz.demo.springCloud.wechat.entity.WeChatApp;
import zz.demo.springCloud.wechat.repository.WeChatAppRepository;
import zz.demo.springCloud.wechat.service.WeChatAppService;
import zz.demo.springCloud.wechat.utils.CommonUtils;
import zz.demo.springCloud.wechat.vo.WeChatAppVO;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangzheng on 2017-11-9.
 */
@Service
public class WeChatAppServiceImpl implements WeChatAppService {
    @Autowired
    private WeChatAppRepository weChatAppRepository;
    @Autowired
    private CommonUtils commonUtils;

    WxError errorPath = WxError.newBuilder().setErrorMsg("None this accessPath").setErrorCode(1).build();
    WxError errorApp = WxError.newBuilder().setErrorMsg("That application is disabled").setErrorCode(1).build();
    WxError errorRepeat = WxError.newBuilder().setErrorMsg("Repeated application!").setErrorCode(1).build();

    /*
        对外调用接口，过滤查询结果。
     */
    @Override
    public WeChatApp findByAccessPath(String accessPath) throws WxErrorException {
        WeChatApp weChatApp = findByAccessPathWithoutFlag(accessPath);
        if(!weChatApp.isEnableFlag()){
            throw new WxErrorException(errorApp);
        }
        return weChatApp;
    }

    @Override
    public void updateByWeChatAppAndAccessPath(WeChatAppVO vo,String accessPath) throws WxErrorException {
        WeChatApp source = findByAccessPathWithoutFlag(accessPath);
        weChatAppRepository.save(convert(vo,source));
    }

    @Override
    public void updateApp(WeChatAppVO vo) {

    }

    @Override
    public void addApp(WeChatAppVO vo) {

    }

    /*
        数据库中拉取全部wechatApp信息。判断accessPath是否重复。
     */
    @Override
    public List<WeChatApp> findAllAndCheck() {
        List<WeChatApp> weChatAppList = weChatAppRepository.findAll();
        if(isAppRepeatByAccessPath(weChatAppList)){
            try {
                throw new WxErrorException(errorRepeat);
            } catch (WxErrorException e) {
                e.printStackTrace();
            }
        }
        return weChatAppList;
    }

    /*
        判断wechatAppList是否重复，根据accessPath进行判断。(较为关键）
        下步可以考虑根据serviceId、agentId进行是否重复判断。
     */
    private boolean isAppRepeatByAccessPath(List<WeChatApp> weChatAppList) {
        ArrayList<Integer> accessPathList = new ArrayList<Integer>();
        for(WeChatApp weChatApp:weChatAppList){
            for(Integer agentId:accessPathList){
                if(agentId.equals(weChatApp.getAgentId())){
                    return true;
                }
            }
            accessPathList.add(weChatApp.getAgentId());
        }
        return false;
    }

    /*
        数据库中查找weChatApp
     */
    private WeChatApp findByAccessPathWithoutFlag(String accessPath) throws WxErrorException {
        if(accessPath==null || "".equals(accessPath)){
            throw new WxErrorException(errorPath);
        }
        WeChatApp weChatApp = weChatAppRepository.findByAccessPath(accessPath);
        if(weChatApp == null){
            throw new WxErrorException(errorPath);
        }
        return weChatApp;
    }

    /*
        ID/updateDate ,不允许修改。(如要修改请重启)
        值为空则不修改。
     */
    private WeChatApp convert(WeChatAppVO vo, WeChatApp source){
        if(commonUtils.isNotNull(vo.getAccessPath())){
            source.setAccessPath(vo.getAccessPath());
        }
        if(commonUtils.isNotNull(vo.getAesKey())){
            source.setAesKey(vo.getAesKey());
        }
        if(commonUtils.isNotNull(vo.getAgentId())){
            source.setAgentId(vo.getAgentId());
        }
        if(commonUtils.isNotNull(vo.getComments())){
            source.setComments(vo.getComments());
        }
        if(commonUtils.isNotNull(vo.getCreateBy())){
            source.setCreateBy(vo.getCreateBy());
        }
        source.setEnableFlag(vo.isEnableFlag());
        if(commonUtils.isNotNull(vo.getName())){
            source.setName(vo.getName());
        }
        if(commonUtils.isNotNull(vo.getSecret())){
            source.setSecret(vo.getSecret());
        }
        if(commonUtils.isNotNull(vo.getToken())){
            source.setToken(vo.getToken());
        }
        if(commonUtils.isNotNull(vo.getCorpId())){
            source.setCorpId(vo.getCorpId());
        }
        if(commonUtils.isNotNull(vo.getServiceId())){
            source.setServiceId(vo.getServiceId());
        }
        return source;
    }

}
